/*
* @(#) Stack.java 2018年02月23日
*
* Copyright 2010 NetEase.com, Inc. All rights reserved.
*/
package com.leo.m1802.d23;

import java.util.concurrent.atomic.AtomicReference;

/**
 *
 * @author xuexiaolei
 * @version 2018年02月23日
 */
public class Stack {
    class Node{
        int value;
        Node next;
        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    AtomicReference<Node> head = new AtomicReference<Node>(null);

    public void push(int value){
        Node curHead;
        Node newHead = new Node(value, null);
        do {
            curHead =  head.get();
            newHead.next = curHead;
        }while (!head.compareAndSet(curHead, newHead));
    }

    public int pop(){
        Node curHead, nextHead=null;
        do {
            curHead = head.get();
            if (curHead!=null) {
                nextHead = curHead.next;
            }
        }while (!head.compareAndSet(curHead, nextHead));
        return curHead.value;
    }
}
